package com.chaoyong.higo.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.chaoyong.higo.R;
import com.chaoyong.higo.bean.Count;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class CountAdapter extends BaseAdapter {

	private ArrayList<Count> data;
	private Activity act;
	private LayoutInflater inflater;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmss");
	private Long[] lon = new Long[100];
	public CountAdapter(Activity act, ArrayList<Count> data) {
		// TODO Auto-generated constructor stub
		this.act = act;
		this.data = data;
		inflater = LayoutInflater.from(act);
	}
	
	public void changeData(ArrayList<Count> data) {
		// TODO Auto-generated method stub
		this.data = data;
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			
	        /*//列表第一项
		    if(position == 0){
		       convertView = inflater.inflate(R.layout.account_header, null);
		        return convertView;
		    }
		            //列表最后一项
		    else if(position == data.size()){
		        convertView = inflater.inflate(R.layout.account_header, null);
		        return convertView;
		    }*/
		    
			convertView = inflater.inflate(R.layout.item_count, null);
			holder.date = (TextView) convertView.findViewById(R.id.item_count_date);
			holder.time = (TextView) convertView.findViewById(R.id.item_count_time);
			holder.username = (TextView) convertView.findViewById(R.id.item_count_username);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		String date1 = data.get(position).getTime();
		String split1 = date1.split("\\.")[0];
		String split2 = date1.split("\\.")[1];
		String date2 = sdf.format(new Date(Long.parseLong(split1)*1000L));
		holder.date.setText(date2 + "." + split2);
		holder.time.setText(sdf2.format(new Date(Long.parseLong(split1) * 1000L)) + split2);
		/*String timetime = data.get(position).getTime();
		String time1 = timetime.split(".")[0];
		holder.date.setText(data.get(position).getTime());
		holder.time.setText(time1);*/
		holder.username.setText(data.get(position).getUser_name());
		return convertView;
	}

	class ViewHolder{
		TextView date,time,username;
	}
}
